/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.satacredito.database.dao;

import co.edu.unal.satacredito.database.entity.Administrator;
import co.edu.unal.satacredito.database.entity.ChangeLogBrand;
import co.edu.unal.satacredito.database.entity.exceptions.NonexistentEntityException;
import com.google.code.morphia.Datastore;
import com.google.code.morphia.query.Query;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Miguel Alexander Diaz 257835
 */
public class ChangeLogBrandMongoDao {

    private Datastore ds = null;

    public ChangeLogBrandMongoDao(Datastore ds) {
        this.ds = ds;
    }

    public Datastore getDatastore() {
        return ds;
    }

    public void create(ChangeLogBrand log) throws Exception {
        try {
            ds.save(log);
        } catch (Exception e) {
            throw e;
        }
    }
    
    public List<ChangeLogBrand> findAll() throws NonexistentEntityException {
        try {
            List<ChangeLogBrand> list = ds.find(ChangeLogBrand.class).order("date").asList();
            return list;
        } catch (Exception e) {
            throw new NonexistentEntityException("The database "
                    + ds.getMongo().getUsedDatabases()
                    + " at " + ds.getMongo().getAddress()
                    + " don't contain any element of the 'ChangeLogBrand' type ");
        }
    }
    
     public List<ChangeLogBrand> findByDate(Date begin, Date end) throws NonexistentEntityException{
        try{
            Query <ChangeLogBrand> q=ds.createQuery(ChangeLogBrand.class).filter("date >=", begin).filter("date <=", end).order("date");
            List <ChangeLogBrand> list=q.asList();
            return list;
        }
        catch(Exception e){
            throw new NonexistentEntityException("The database "
                    + ds.getMongo().getUsedDatabases()
                    + " at " + ds.getMongo().getAddress()
                    + " don't contain any element between the date "
                    + begin.toString() + " and " + end.toString()+ ".");
        }
    }
     
     public List<ChangeLogBrand> findByAdministrator(Administrator admin) throws NonexistentEntityException{
        try{
            Query <ChangeLogBrand> q=ds.createQuery(ChangeLogBrand.class).filter("administrator =", admin).order("date");
            List <ChangeLogBrand> list=q.asList();
            return list;        
        }
        catch(Exception e){
            throw new NonexistentEntityException("The database "
                    + ds.getMongo().getUsedDatabases()
                    + " at " + ds.getMongo().getAddress()
                    + " don't contain any element asociated with the administrator "
                    + admin.getName()+ ".");
        }
    }
}
